package middleware.p2p.log;

import java.net.URL;

import middleware.p2p.global.logger.GlobalLogger;
import middleware.p2p.global.logger.GlobalLoggerService;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.xml.XMLLayout;

/**
 * 	Layout del logger del sistema middleware
 */
public class MiddlewareLogLayout extends XMLLayout {
	/**
	 * 	Id del componente che usera' il layout
	 */
	private String componentID;
	/**
	 * 	Istanza del Web Service di logging a cui mandare i log
	 */
	private GlobalLogger globalLogger;
	public MiddlewareLogLayout(String cid,URL loggerWsdl) {
		super();
		this.componentID=cid;
		GlobalLoggerService serv=new GlobalLoggerService(loggerWsdl);
		this.globalLogger=serv.getGlobalLoggerPort();		
	}
	public String format(LoggingEvent event) {
		if (event.getMessage() instanceof MiddlewareLogEvent) {			
			MiddlewareLogEvent mwEvent= (MiddlewareLogEvent)event.getMessage();			
			mwEvent.setComponentID(this.componentID);
			mwEvent.setTimestamp(event.getTimeStamp());
			this.globalLogger.pushAsync(mwEvent);
			return mwEvent.toXmlString()+"\n\n";			
		} else {
			//System.out.println("=============> "+event.getMessage());
			return "";
		}
	}

}
